const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startDate:'',
    endDate:'',
    date_popup_show:false,
    root:app.domain,
    tip_info:{},
    active:0,//Tabar索引值
    page:1,//页数
    be_test_list:[],//待考试数据
    tested_list:[],//已考试数组
    keyword:'',//搜索关键词
    test_show:false,//考试提醒弹出层,
    sort_show:false,
    sortOption: [],
    sort:'',
    filter_show:false,
    timeOption:[],
    stateOption:[],
    timeTag:'',//选择的时间筛选
    stateTag:'',
  },


  
  
  /**开始时间选择 */
  bindStartDateChange(e){
    this.setData({
      startDate: e.detail.value
    })
  },
  /**结束时间选择 */
  bindEndDateChange(e){
    this.setData({
      endDate: e.detail.value
    })
  },
  /**自定义时间选择确认 */
  dateSearch(){
    this.setData({
      commit_time:'',
      date_popup_show: false
    })
    this.requestData();
  },


  /**开始考试 */
  startExam(e){
    let id = e.currentTarget.dataset.id;
    if(!api.isBlank(id)){
      this.setData({
        test_show: false,
      })
      if(this.data.tip_info.answerType=='整卷答题'){
        wx.navigateTo({
          url: '../paper/paper?id='+id,
        })
      }else if(this.data.tip_info.answerType=='逐题答题'){
        wx.navigateTo({
          url: '../paperOne/paperOne?id='+id,
        })
      }
    }
  },
  condition(){
    this.setData({
      filter_show:true,
    })
  },
  /**取消弹出层 */
  cancelTip(e){
    this.setData({
      test_show: false,
    });
  },
  /**搜索文字变动 */
  searchChange(e){
    this.setData({
      keyword: e.detail,
    });
  },
  onSearch(){
    this.requestData();
  },
  /**点击待考试试题 */
  detail(e){
    let that = this;
    let id = e.currentTarget.dataset.id;
    api.curlPost('?cla=paperManagerApp&fun=answerTip',{id:id}).then((res)=>{
      that.setData({
        test_show: true,
        tip_info:res.data.result
      });
    })
   
  },
  /**底部tabar切换 */
  onChange(event){
    let that = this;
    let index = event.detail;
    that.setData({
      active: index,
    })
    this.requestData()
  },
  chooseTimeTag(e){
    let value = e.currentTarget.dataset.value;
    if(value=='自定义'){
      this.setData({
        timeTag:value,
        date_popup_show:true,
      })
      this.cancelFilter();
    }else{
      if(value ==this.data.timeTag){
        value = '';
      }
      this.setData({
        timeTag:value
      })
    }
    
  },
  chooseStateTag(e){
    let value = e.currentTarget.dataset.value;
    if(value ==this.data.stateTag){
      value = '';
    }
   
    this.setData({
      stateTag:value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    this.requestData();
    api.curlPost('?cla=paperManagerApp&fun=search').then((res)=>{
      that.setData({
        sortOption:res.data.sortOption,
        sort:res.data.defualtSort,
        stateOption:res.data.stateOption,
        timeOption:res.data.timeOption
      })
      
    })
  },

  /**详情 */
  record(e){
    wx.navigateTo({
      url: '../paperRecord/paperRecord?id='+e.currentTarget.dataset.id,
    })

  },
  /**
   * 展开排序
   */
  showSortPopup(){
  
    this.setData({
      sort_show:true,
    })
    
  },
  /**关闭排序 */
  onSortClose(){
    this.setData({
      sort_show:false
    })
  },
  onCellClick(e){
    this.setData({
      sort:e.currentTarget.dataset.index,
      sort_show:false
    })
    this.requestData();
  },
  onRadioChange(event) {
    this.setData({
      sort: event.detail,
      sort_show:false
    });
    this.requestData();
  },
  cancelFilter(){
    this.setData({
      filter_show:false
    })
  },
  resetTage(){
    this.setData({
      timeTag:'',
      stateTag:''
    })
  },
  confirmFilter(){
    this.setData({
      startDate:'',
      endDate:'',
    })
    this.requestData();
    this.cancelFilter();
  },
  /**
   * 请求数据
   */
  requestData:function(page=1){
    let that = this;
    let active = this.data.active;
    if(active==0){
      let url = '?cla=paperManagerApp&fun=beTested&page='+page
      api.curlPost(url).then((res)=>{
        if(!api.isBlank(res.data.result) || page ==1){
          let arr = page==1?res.data.result:that.data.be_test_list.concat(res.data.result);
          that.setData({
            be_test_list:arr,
            page:Number(res.data.page.pageNow) +1
          })
        }
      })
    }else if(active==1){
      let  url = '?cla=paperManagerApp&fun=tested&page='+page
      let para = {
        keyword:this.data.keyword,
        sort:this.data.sort,
        chooseTime:this.data.timeTag,
        state:this.data.stateTag,
        startDate:this.data.startDate,
        endDate:this.data.endDate
      }
      api.curlPost(url,para).then((res)=>{
        if(!api.isBlank(res.data.result) || page ==1){
          let arr = page==1?res.data.result:that.data.tested_list.concat(res.data.result);
          that.setData({
            tested_list:arr,
            page:Number(res.data.page.pageNow) +1
          })
        }
      })
    }
    
    
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.requestData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})